﻿/*** MikeWare Framework ************************************
* This class is part of product of MikeWare.Framework.
* 
* Author      : Mike Cheers
* Mail        : mikecheers@126.com
* Create Date : 2018/9/18
* Summary     : 
* 
* 
* Modified By : 
* Date        : 
* Mail        : 
* Comment     : 
**************************************************************/

namespace MikeWare.Core.WinApiWrappers
{
    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;

    public static class WDMAPIWrapper
    {
        [DllImport("DllRefs\\dwmapi.dll", EntryPoint = "DwmIsCompositionEnabled", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int DwmIsCompositionEnabled(ref bool enabled);

        [DllImport("DllRefs\\dwmapi.dll", EntryPoint = "DwmEnableComposition", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int DwmEnableComposition(DWM.EC wdm_ec);

        [DllImport("DllRefs\\dwmapi.dll", EntryPoint = "DwmGetWindowAttribute", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int DwmGetWindowAttribute(IntPtr hwnd, WDMAPIWrapper.DWM.DWMWINDOWATTRIBUTE dwAttribute, out WDMAPIWrapper.DWM.DWMNCRENDERINGPOLICY pvAttribute, int cbAttribute);

        [DllImport("DllRefs\\dwmapi.dll", EntryPoint = "DwmSetWindowAttribute", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int DwmSetWindowAttribute(IntPtr hwnd, int dwAttribute, IntPtr pvAttribute, int cbAttribute);

        [DllImport("DllRefs\\dwmapi.dll", EntryPoint = "DwmExtendFrameIntoClientArea", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref DWM.MARGINS pMarinset);

        [DllImport("DllRefs\\dwmapi.dll", EntryPoint = "DwmEnableBlurBehindWindow", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int DwmEnableBlurBehindWindow(IntPtr hWnd, ref DWM.BLURBEHIND pBlurBehind);

        public class DWM
        {
            [StructLayout(LayoutKind.Sequential)]
            public struct MARGINS
            {
                public int Right;
                public int Left;
                public int Top;
                public int Bottom;
            }

            [StructLayout(LayoutKind.Sequential)]
            public struct BLURBEHIND
            {
                public BB dwFlags;
                public bool fEnable;
                public IntPtr hRgnBlur;
                public bool fTransitionOnMaximized;
            }

            public enum BB : uint
            {
                BB_ENABLE = 0x00000001,
                BB_BLURREGION = 0x00000002,
                BB_TRANSITIONONMAXIMIZED = 0x00000004
            }

            public enum EC : uint
            {
                DWM_EC_DISABLECOMPOSITION = 0x00000000,
                DWM_EC_ENABLECOMPOSITION = 0x00000001
            }

            public enum DWMWINDOWATTRIBUTE
            {
                DWMWA_NCRENDERING_ENABLED = 1,
                DWMWA_NCRENDERING_POLICY,
                DWMWA_TRANSITIONS_FORCEDISABLED,
                DWMWA_ALLOW_NCPAINT,
                DWMWA_CAPTION_BUTTON_BOUNDS,
                DWMWA_NONCLIENT_RTL_LAYOUT,
                DWMWA_FORCE_ICONIC_REPRESENTATION,
                DWMWA_FLIP3D_POLICY,
                DWMWA_EXTENDED_FRAME_BOUNDS,
                DWMWA_HAS_ICONIC_BITMAP,
                DWMWA_DISALLOW_PEEK,
                DWMWA_EXCLUDED_FROM_PEEK,
                DWMWA_CLOAK,
                DWMWA_CLOAKED,
                DWMWA_FREEZE_REPRESENTATION,
                DWMWA_LAST
            }

            public enum DWMNCRENDERINGPOLICY
            {
                DWMNCRP_USEWINDOWSTYLE,
                DWMNCRP_DISABLED,
                DWMNCRP_ENABLED,
                DWMNCRP_LAST
            }
        }

        internal class DwmApi
        {
            [DllImport("dwmapi.dll", PreserveSig = false)]
            public static extern void DwmEnableBlurBehindWindow(IntPtr hWnd, DWM_BLURBEHIND pBlurBehind);
            [DllImport("dwmapi.dll", PreserveSig = false)]
            public static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, MARGINS pMargins);
            [DllImport("dwmapi.dll", PreserveSig = false)]
            public static extern bool DwmIsCompositionEnabled();
            [DllImport("dwmapi.dll", PreserveSig = false)]
            public static extern void DwmEnableComposition(bool bEnable);
            [DllImport("dwmapi.dll", PreserveSig = false)]
            public static extern void DwmGetColorizationColor(out int pcrColorization, [MarshalAs(UnmanagedType.Bool)]out bool pfOpaqueBlend);
            [DllImport("dwmapi.dll", PreserveSig = false)]
            public static extern IntPtr DwmRegisterThumbnail(IntPtr dest, IntPtr source);
            [DllImport("dwmapi.dll", PreserveSig = false)]
            public static extern void DwmUnregisterThumbnail(IntPtr hThumbnail);
            [DllImport("dwmapi.dll", PreserveSig = false)]
            public static extern void DwmUpdateThumbnailProperties(IntPtr hThumbnail, DWM_THUMBNAIL_PROPERTIES props);
            [DllImport("dwmapi.dll", PreserveSig = false)]
            public static extern void DwmQueryThumbnailSourceSize(IntPtr hThumbnail, out Size size);
            [StructLayout(LayoutKind.Sequential)]
            public class DWM_THUMBNAIL_PROPERTIES
            {
                public uint dwFlags; public RECT rcDestination; public RECT rcSource; public byte opacity;
                [MarshalAs(UnmanagedType.Bool)]
                public bool fVisible;
                [MarshalAs(UnmanagedType.Bool)]
                public bool fSourceClientAreaOnly; public const uint DWM_TNP_RECTDESTINATION = 0x00000001; public const uint DWM_TNP_RECTSOURCE = 0x00000002; public const uint DWM_TNP_OPACITY = 0x00000004; public const uint DWM_TNP_VISIBLE = 0x00000008; public const uint DWM_TNP_SOURCECLIENTAREAONLY = 0x00000010;
            }
            [StructLayout(LayoutKind.Sequential)]
            public class MARGINS
            {
                public int cxLeftWidth, cxRightWidth, cyTopHeight, cyBottomHeight;
                public MARGINS(int left, int top, int right, int bottom)
                {
                    cxLeftWidth = left; cyTopHeight = top; cxRightWidth = right; cyBottomHeight = bottom;
                }
            }
            [StructLayout(LayoutKind.Sequential)]
            public class DWM_BLURBEHIND
            {
                public uint dwFlags;
                [MarshalAs(UnmanagedType.Bool)]
                public bool fEnable; public IntPtr hRegionBlur;
                [MarshalAs(UnmanagedType.Bool)]
                public bool fTransitionOnMaximized; public const uint DWM_BB_ENABLE = 0x00000001; public const uint DWM_BB_BLURREGION = 0x00000002; public const uint DWM_BB_TRANSITIONONMAXIMIZED = 0x00000004;
            }
            [StructLayout(LayoutKind.Sequential)]
            public struct RECT
            {
                public int left, top, right, bottom;
                public RECT(int left, int top, int right, int bottom)
                {
                    this.left = left; this.top = top; this.right = right; this.bottom = bottom;
                }
            }
        }
    }
}
